package com.caterina.activities;
import java.awt.Container;
import java.util.ArrayList;
import java.util.Collection;

import person.Customer;
import person.Person;
import modulesAPI.v2.CollectionButtonListener;
import modulesAPI.v2.Module;
import modulesAPI.v2.ModuleChangedListener;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCollectionViewer;
import modulesAPI.v2.composite.MRequest;
import budget.Request;
import com.caterina.activityManager.CActivity;
import com.caterina.components.CButton;
import com.caterina.components.COptionPane.MessageType;
import com.caterina.resources.CWords;
import controllers.ManagerTask.TaskType;
import controllers.Permission;
import controllers.PermissionList;

public class RequestsActivity extends CActivity implements ModuleChangedListener,CollectionButtonListener{
	public RequestsActivity(Container parent) {
		super("REQUESTS_ACTIVITY_TITLE",parent);
		super.setCBarVisible(true);
		collectionView = null;
	}
	@Override
	public void onStart() {
		this.putTask(TaskType.USER_PERMISSIONS);
	}
	@SuppressWarnings("unchecked")
	@Override
	public void onData(TaskType task, Object data) {
		switch(task){
			case USER_PERMISSIONS:{
				this.permissions=new PermissionList((Collection<Permission>) data);
				this.putTask(TaskType.USER_CURRENT);
				break;
			}
			case USER_CURRENT:{
				this.currentUser=(Person) data;
				this.putTask(TaskType.REQUEST_GET_ALL, "");
				break;
			}
			case REQUEST_GET_ALL:{
				if(this.collectionView!=null)this.collectionView.setAddMode(false);
				this.lastData=(Collection<Request>) data;
				this.reloadViewer((Collection<Request>) data);
				break;
			}
			case REQUEST_SEND:{
				this.createAddModule();
				this.putTask(TaskType.REQUEST_GET_ALL, "");
				break;
			}
			default:this.putTask(TaskType.REQUEST_GET_ALL, "");
		}
	}
	@Override
	public void onError(TaskType task, Exception e) {
		this.showCOPtionPane(MessageType.ERROR, CWords.getWord(e.getMessage()));
	}
	
	private void createButtons(){
		/*Tasto accetta*/
		this.accept=new CButton("REQUEST_ACCEPT") {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				startLoading();
				putTask(TaskType.REQUEST_ACCEPT,collectionView.getSelectedContent().getContent());}
			private static final long serialVersionUID = 1L;
		};
		/*Tasto rifiuto*/
		this.deny=new CButton("REQUEST_DENY") {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				startLoading();
				putTask(TaskType.REQUEST_DENY,collectionView.getSelectedContent().getContent(),"");
				}
			private static final long serialVersionUID = 1L;
		};
		/*Tasto invia*/
		this.send=new CButton("REQUEST_SEND") {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				startLoading();
				putTask(TaskType.REQUEST_SEND, addModule.getContent());
				}
			private static final long serialVersionUID = 1L;
		};
		/*Tasto annulla*/
		this.cancel=new CButton("REQUEST_CANCEL") {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				collectionView.setAddMode(false);
				addModule=new MRequest(null, "NEW_REQUEST", "NEW_REQUEST_DESC", ViewType.WRITABLE);
				createButtons();
				addModule.addWritableButton(send,cancel);
				reloadViewer(lastData);
				}
			private static final long serialVersionUID = 1L;
		};
	}
	private void createAddModule(){
		if (this.permissions.can(Permission.REQUESTS_CREATE_ALL))
			this.addModule=new MRequest(null, "NEW_REQUEST", "NEW_REQUEST_DESC", ViewType.WRITABLE);
		else
			this.addModule=new MRequest(null,(Customer)this.currentUser,"NEW_REQUEST", "NEW_REQUEST_DESC", ViewType.WRITABLE,MRequest.CUSTOMER_MODE);
		this.createButtons();
		this.addModule.addWritableButton(send,cancel);
	}
	
	private void reloadViewer(Collection<Request> data){
		boolean addModeSetted=false;
		if (this.collectionView!=null){
			addModeSetted=this.collectionView.isInAddMode();
			this.collectionView.removeModuleChangedListener(this);
			this.collectionView.removeCollectionButtonListener(this);
		}
		if (this.addModule==null)this.createAddModule();
		ArrayList<Module<?>> reqModules=new ArrayList<Module<?>>();
		for (Request req: data){
			MRequest reqMod=new MRequest(null, req,"REQUEST", "REQUEST_DESC", ViewType.READ_ONLY);
			if (this.permissions.can(Permission.REQUESTS_MANAGE))
				reqMod.addButtons(accept,deny);
			reqModules.add(reqMod);
		}
		this.collectionView=new ModCollectionViewer(null,reqModules ,addModule, "NO_CONTENT", "NO_CONTENT", ViewType.WRITABLE);
        this.collectionView.addModuleChangedListener(this);
        this.collectionView.addCollectionButtonListener(this);
        this.collectionView.setAddMode(addModeSetted);
		this.add(collectionView);
	}
	@Override
	public void onButtonPressed(Module<?> selectedItem, int buttonIndex) {
		if (buttonIndex==ModCollectionViewer.REMOVE)
			this.putTask(TaskType.REQUEST_REMOVE, selectedItem.getContent());
	}
	@Override
	public void onModuleChange(Module<?> mod) {
		this.add(collectionView);
	}
	private Person currentUser;
	private PermissionList permissions;
	private Collection<Request>lastData;
	private CButton accept;
	private CButton deny;
	private CButton send;
	private CButton cancel;
	private MRequest addModule;
	private ModCollectionViewer collectionView;
	private static final long serialVersionUID = 1L;
}
